import i18n from 'i18next';

export interface languageState {
    language: 'zh' | 'en';
    languageList: { name: string; code: string }[];
}

const defaultState: languageState = {
    language: 'zh',
    languageList: [
        { name: '中文', code: 'zh' },
        { name: 'English', code: 'en' },
        { name: '新增语言', code: 'add' }
    ]
};

const reducer = (state = defaultState, action) => {
    switch (action.type) {
        case 'CHANGELANGUAGE': // 切换语言
            i18n.changeLanguage(action.value);
            return { ...state, language: action.value }; // 不能直接修改旧的state，而是返回一个新的state来替代
        case 'ADDLANGUAGE': // 新增语言
            return { ...state, languagelist: [...state.languageList, action.value] };
        default:
            return state;
    }
};

export default reducer;
